#!/usr/bin/env python
#coding=gbk
from mutagen.easyid3 import EasyID3
from mutagen.mp3 import MP3
from pprint import pprint
import re
import sys
import mutagen

def tryCorrectCodec(xx):
    gb=""
    for a in xx:
        tmp=ord(a)
        gb+=chr(tmp)
    return gb.decode("GBK")


def getItem(value):
    xx=value
    try:
        temp=xx.encode("GBK")
    except:
        try:
            xx=tryCorrectCodec(xx)
        except:
            xx=u""
    return xx

def repair(name):
    audio = MP3(name, ID3=EasyID3)
    for i in audio.iteritems():
        pprint( getItem(i[1][0]))
        audio[i[0]]=getItem(i[1][0])
    audio.save()

import os,sys
path="g:\\KuGou\\"
for root, dirs, files in os.walk(path):
  for file in files:
    match=re.match(".*\.mp3",file)
    if match:
        path=os.path.join(root, file)
        if type(path)==type("str"):
            path=path.decode("GBK")
            try:
                repair(path,"GBK")
            except Exception,e:
                pass


